package com.ssi.action.customer;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ssi.annotation.Premission;
import com.ssi.entity.customer.Customer;
import com.ssi.service.customer.ICustomerService;
import com.ssi.utils.Constants;
import com.ssi.utils.StringUtil;

@Controller
@RequestMapping(value="/customer")
public class CustomerLoginAction {

	@Autowired
	@Qualifier("customerService")
	private ICustomerService customerService;
	
	@Autowired(required=false)
	Validator validator;
	
	@Autowired(required=false)
	ResourceBundleMessageSource messageSource;
	
	@Premission(item="1000001")
	@RequestMapping(value = "/login")
	public String Login(@ModelAttribute("customer") Customer customer,BindingResult bindingResult,HttpServletRequest request, HttpServletResponse response, Model model){
		try{
			if(StringUtil.isNullOrEmpty(customer.getUserEmail())){
				request.setAttribute("userEmailMsg", "邮箱不能为空");
			}
			if(StringUtil.isNullOrEmpty(customer.getUserEmail())){
				request.setAttribute("userPassMsg", "密码不能为空");
			}
			if(StringUtil.isNullOrEmpty(customer.getUserEmail()) || StringUtil.isNullOrEmpty(customer.getUserEmail())){
				return "/customer/customer-login";
			}
			List<Customer> customerList = customerService.checkUser(customer);
			if(customerList == null ){
				return "/customer/customer-login";
			}
			if(customerList.size() == 1){
				Customer cm = customerList.get(0);
				HttpSession session = request.getSession();
				session.setAttribute(Constants.USER_SESSION, cm);
			}else{
				request.setAttribute("userEmailMsg", "该邮箱或者密码错误");
				return "/customer/customer-login";
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return "/index/index";
	}
	
	@Premission(item="1000002")
	@RequestMapping(value = "/toregister")
	public String toRegister(@Valid @ModelAttribute("customer") Customer customer,BindingResult bindingResult,HttpServletRequest request, HttpServletResponse response, Model model){
		return "/customer/customer-register";
	}
	
	@Premission(item="1000003")
	@RequestMapping(value = "/register")
	public String register(@Valid @ModelAttribute("customer") Customer customer,BindingResult bindingResult,HttpServletRequest request, HttpServletResponse response, Model model){
		try{
			validator.validate(customer, bindingResult);
			if(bindingResult.hasErrors()){
				return "/customer/customer-register";
			}else{
				List<Customer> customerList = customerService.queryCustomerListByUserName(customer.getUserName());
				if(customerList.size() > 0){
					request.setAttribute("userNameMsg", "该用户名已被注册");
					return "/customer/customer-register";
				}
				List<Customer> customerList1 = customerService.queryCustomerListByEmail(customer.getUserEmail());
				if(customerList1.size() > 0){
					request.setAttribute("userEmailMsg", "该邮箱已被注册");
					return "/customer/customer-register";
				}
				Long customerId = customerService.insertCustomer(customer);
			}
		}catch(Exception e){
			e.printStackTrace();
			//return "error";
		}
		return "redirect:/customer/login";
	}
}
